module(
    name = "gazelle_bcr_go_mod_tests",
)

bazel_dep(name = "gazelle", version = "")
local_path_override(
    module_name = "gazelle",
    path = "../../..",
)

bazel_dep(name = "test_dep", version = "1.0.0")
local_path_override(
    module_name = "test_dep",
    path = "test_dep",
)

bazel_dep(name = "bazel_features", version = "1.27.0")
bazel_dep(name = "protobuf", version = "27.0", repo_name = "my_protobuf")
bazel_dep(name = "rules_go", version = "0.59.0", repo_name = "my_rules_go")
bazel_dep(name = "rules_license", version = "1.0.0")
bazel_dep(name = "package_metadata", version = "0.0.5")
bazel_dep(name = "rules_proto", version = "6.0.0", repo_name = "my_rules_proto")
bazel_dep(name = "rules_testing", version = "0.6.0")

go_sdk = use_extension("@my_rules_go//go:extensions.bzl", "go_sdk")
go_sdk.download(version = "1.24.2")

# This bazel_dep provides the Go dependency github.com/cloudflare/circl, which requires custom
# patches beyond what Gazelle can generate.
bazel_dep(name = "circl", version = "1.3.8")

go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
go_deps.config(
    debug_mode = True,
    go_env = {
        "GOPRIVATE": "example.com/*",
    },
)

# Validate a go.mod replace directive works.
go_deps.from_file(go_mod = "//:go.mod")
go_deps.gazelle_default_attributes(
    build_file_generation = "on",
    directives = [
        "gazelle:proto disable",
    ],
)

# By defining `gazelle_default_attributes`, we also must individually
# specify certain overrides from internal/bzlmod/default_gazelle_overrides.bzl
#
# "build_file_generation" defaults to "on" because we provided a "gazelle_override"
# (which contains either directives or build extra args).
go_deps.gazelle_override(
    directives = [
        "gazelle:build_file_name BUILD.bazel",
        "gazelle:build_file_proto_mode disable_global",
    ],
    path = "github.com/google/safetext",
)
go_deps.gazelle_override(
    directives = [
        "gazelle:build_file_name BUILD.bazel",
    ],
    path = "github.com/envoyproxy/protoc-gen-validate",
)

# Verify that the gazelle:go_naming_convention directive in an override is
# respected.
go_deps.module(
    path = "github.com/stretchr/testify",
    sum = "h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=",
    version = "v1.8.0",
)
go_deps.gazelle_override(
    directives = [
        "gazelle:go_naming_convention go_default_library",
    ],
    path = "github.com/stretchr/testify",
)

# Apply a patch to test the `module_override` tags.
go_deps.module_override(
    patch_strip = 1,
    patches = [
        "//patches:testify.patch",
    ],
    path = "github.com/stretchr/testify",
)

# Test an archive override from a known archive.
go_deps.gazelle_override(
    build_file_generation = "clean",
    path = "github.com/bazelbuild/buildtools",
)
go_deps.archive_override(
    patch_strip = 1,
    patches = [
        "//patches:buildtools.patch",
    ],
    path = "github.com/bazelbuild/buildtools",
    sha256 = "05d7c3d2bd3cc0b02d15672fefa0d6be48c7aebe459c1c99dced7ac5e598508f",
    strip_prefix = "buildtools-ae8e3206e815d086269eb208b01f300639a4b194",
    urls = [
        "https://github.com/bazelbuild/buildtools/archive/ae8e3206e815d086269eb208b01f300639a4b194.tar.gz",
    ],
)

# Test a module that ships with its own BUILD files and first needs rewriting
go_deps.gazelle_override(
    build_file_generation = "clean",
    path = "github.com/google/go-jsonnet",
)

# Transitive dependencies have to be listed here explicitly.
go_deps.module(
    indirect = True,
    path = "gopkg.in/yaml.v3",
    sum = "h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=",
    version = "v3.0.1",
)
go_deps.gazelle_override(
    directives = [
        # Verify that the build naming convention is picked up by Gazelle when it
        # emits references to this repo.
        "gazelle:go_naming_convention go_default_library",
    ],
    path = "gopkg.in/yaml.v3",
)
go_deps.module(
    indirect = True,
    path = "github.com/davecgh/go-spew",
    sum = "h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=",
    version = "v1.1.1",
)
use_repo(
    go_deps,
    # Only used for testing.
    "bazel_gazelle_go_repository_config",
    "cc_mvdan_gofumpt",
    "co_honnef_go_tools",
    "com_github_bazelbuild_buildtools",
    "com_github_bmatcuk_doublestar_v4",
    "com_github_datadog_sketches_go",
    "com_github_envoyproxy_protoc_gen_validate",
    "com_github_fmeum_dep_on_gazelle",
    "com_github_google_go_jsonnet",
    "com_github_google_safetext",
    "com_github_stretchr_testify",
    "org_example_hello",
    "org_golang_google_genproto",
    "org_golang_google_genproto_googleapis_bytestream",
    "org_golang_google_protobuf",
    "org_golang_x_sys",
)

# Use an isolated usage to bring in Go tools from the tools package with their own dependency
# closure.
go_tools = use_extension("@gazelle//:extensions.bzl", "go_deps", isolate = True)
go_tools.from_file(go_mod = "//tools:go.mod")
go_tools.gazelle_override(
    build_file_generation = "clean",
    directives = [
        "gazelle:proto disable",
    ],
    path = "github.com/bazelbuild/buildtools",
)
use_repo(
    go_tools,
    buildtools = "com_github_bazelbuild_buildtools",
    gqlgen = "com_github_99designs_gqlgen",
    unused = "bazel_gazelle_go_repository_config",
)
